package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.TrainTanker;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.TrainWagon;
import cn.eonshine.cuba.lsms.modular.basic.service.ITrainWagonClientService;
import cn.eonshine.cuba.lsms.modular.basic.service.ITrainWagonService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
@Service
public class TrainWagonClientService extends CommonService<TrainTanker> implements ITrainWagonClientService {
	
	@Autowired
	private ITrainWagonService iTrainWagonService;
	
	private final String listUrl = "/trainTanker/list";
	private final String updateUrl = "/trainTanker/update";
	
	@Override
	public void requestTrainWagon() {
		requestData(listUrl, updateUrl);
	}
	
	@Override
	protected Object dataHandle(TrainTanker trainTanker) throws Exception {
		QueryWrapper<TrainWagon> entity = new QueryWrapper<TrainWagon>();
		entity.eq("ID", trainTanker.getCode());
		TrainWagon info = iTrainWagonService.getOne(entity);
		boolean isInsert = false;
		if(info == null) {
			info = new TrainWagon();
			isInsert = true;
		}
		
		info.setId(trainTanker.getCode());
		BeanUtil.copyProperties(trainTanker, info,true, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
		info.setCreateBy(InfStatus.CREATE_BY);
		if(info.getCreateDate() == null) {
			info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		}
		info.setUpdateBy(InfStatus.UPDATE_BY);
		if(info.getUpdateDate() == null) {
			info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		}
		
		if(isInsert) {
			iTrainWagonService.save(info);
		}else {
			iTrainWagonService.updateById(info);
		}
		
		return info;
	}
	@Override
	protected List<TrainTanker> feadBackDataHandle(Object obj, TrainTanker trainTanker, List<TrainTanker> feadBackDataLists) throws Exception {
		TrainWagon info = (TrainWagon) obj;
		TrainTanker tmpBack = new TrainTanker();
		tmpBack.setId(info.getId());
		tmpBack.setCode(trainTanker.getCode());
		tmpBack.setDataInputDatetime(trainTanker.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
